#include <gtk/gtk.h>

//simple callback function, arguments are ignored.
static void hello ( GtkWidget *widget, gpointer data)
{
    g_print ("Hello World\n");
}

//delete event handler. returning FALSE from this will emit the destroy signal and call that handler 
static gboolean delete_event ( GtkWidget *widget,
			       GdkEvent *event,
			       gpointer data )
{
    g_print ("delete event occurred");
    return TRUE;
}
    
//callback function for the destroy event
static void destroy ( GtkWidget *widget,
		      gpointer  data )
{
    gtk_main_quit ();
}

//program entry point
int main ( int  argc,
	   char *argv[] )
{	   
    //GtkWidget is the storage type for widgets
    GtkWidget *window;
    GtkWidget *button;

    //gtk_init is called in all GTK applications. cmdline arguments are parsed and returned
    gtk_init (&argc, &argv);

    //create a new window, at TOPLEVEL which uses default window handler
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    //signal connecting... when the window is given the "delete_event" SIGNAL by window manager, we
    //pair that event to call the delete_event function. Arguments passed are NULL in this case
    g_signal_connect (G_OBJECT (window), "delete_event",
		      G_CALLBACK (delete_event), NULL);

    //signal connecting... we also need to connect the "destroy" SIGNAL (event handler). this event
    //is fired when 1. we call gtk_widget_destroy() on the window, or 2. we return FALSE in the
    //delete_event callback
    g_signal_connect (G_OBJECT (window), "destroy",
		      G_CALLBACK (destroy), NULL);

    //this sets a border width of the window
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    //creates a new button with a label
    button = gtk_button_new_with_label ("I love puppy!");
    
    //signal connecting... when the button recieves the "clicked" SIGNAL it will call the function hello()
    g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (hello), NULL);
    
    //this will cause the window to be destroyed by calling gtk_widget_destroy(window) when "clicked" it
    //calls the callback function with argument G_OBJECT(window) instead of null
    g_signal_connect_swapped (G_OBJECT (button), "clicked",
			      G_CALLBACK (gtk_widget_destroy),
			      G_OBJECT (window));

    //pack the button into the window (which is a gtk container)
    gtk_container_add (GTK_CONTAINER (window), button);

    //display the widget then the window (container
    gtk_widget_show (button);
    gtk_widget_show (window);

    //all GTK applications have to call gtk_main, all control ends and waits for events to occur
    gtk_main();

    return 0;
}
    




